﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using log4net;

namespace RollbackLib.src.sys
{
    /// <summary>
    /// Control the sleep mode of windows.
    /// </summary>
    public class PowerState
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(PowerState));

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);

        [FlagsAttribute]
        public enum EXECUTION_STATE : uint
        {
            ES_SYSTEM_REQUIRED = 0x00000001,
            ES_DISPLAY_REQUIRED = 0x00000002,
            // Legacy flag, should not be used.
            // ES_USER_PRESENT   = 0x00000004,
            ES_CONTINUOUS = 0x80000000,
        }

        public static void PreventSystemPowerdown()
        {
            SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
            log.Info("Previnting power save mode");
        }

        public static void AllowSystemPowerdown()
        {
            SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
            log.Info("Entering system default power save mode");
        }
    }
}
